﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using RuoVea.ExConfig;
using RuoVea.ExDto;
using RuoVea.ExDto.Language;
using RuoVea.ExEnum;
using RuoVea.ExIdGen;
using RuoVea.ExSugar;
using RuoVea.ExUtil;

namespace RuoVea.H5ds.Web.Controllers;

[Authorize(Roles = "None")]
public partial class H5DSController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<H5Template> _thisRepository;
    private readonly SugarRepository<H5ServerInfo> _serverInfoRepository;
    private readonly ICurrentUser _currentUser;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param>
    /// <param name="currentUser"></param>
    /// <param name="serverInfoRepository"></param>
    public H5DSController(SugarRepository<H5Template> thisRepository, ICurrentUser currentUser, SugarRepository<H5ServerInfo> serverInfoRepository)
    {
        _thisRepository = thisRepository;
        _currentUser = currentUser;
        _serverInfoRepository = serverInfoRepository;
    }

    public IActionResult Index(string appId)
    {
        ViewBag.appId = appId;
        return View();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("H5DS/data/{id:long}")]
    public async Task<RestfulResult<H5Template>> GetDataAsync(long id)
    {
        RestfulResult<H5Template> restful = new() { Code = CodeStatus.BadRequest };

        if (id == 0)
        {
            restful.Message = "失败";
            return restful;
        }
        H5Template data = await _thisRepository.GetFirstAsync(x => x.Id == id);
        data.ResultContent = "";
        if (data.Content.IsNullOrWhiteSpace())
        {
            var defultData = (await _serverInfoRepository.GetFirstAsync(x => x.Code == "defultData"))?.Content;
            if (defultData.IsNullOrWhiteSpace()||defultData=="{}")
            {
                //data.Content = @"{""version"":""5.0.0"",""img"":""/lib/bgimg/bg.jpg"",""desc"":""浅语书畔"",""name"":""浅语书畔"",""type"":""phone"",""slider"":{""speed"":0.5,""effect"":""slide"",""autoplay"":false,""time"":5},""style"":{""width"":320,""height"":514},""fixeds"":[{""id"":null,""className"":null,""keyid"":""1yaqkl5trbhvcw"",""name"":""浮动层上"",""style"":{""height"":514,""width"":320},""layers"":[]},{""id"":null,""className"":null,""keyid"":""2xdp378n1aylmo"",""name"":""浮动层下"",""style"":{""height"":514,""width"":320},""layers"":[]}],""popups"":[],""pages"":[{""id"":null,""className"":null,""keyid"":""3211mt6rpwdl34"",""name"":""空白页面"",""desc"":""页面描述"",""style"":{""width"":320,""height"":514},""layers"":[],""slider"":{""animate"":1,""autoplay"":false,""lock"":false,""time"":5}}]}";
                data.Content = @"{""version"":""5.0.0"",""img"":""/img/bg.jpg"",""desc"":""浅语书畔"",""name"":""浅语书畔"",""type"":""phone"",""slider"":{""speed"":0.5,""effect"":""slide"",""autoplay"":false,""time"":5},""style"":{""width"":320,""height"":514},""fixeds"":[{""id"":null,""className"":null,""keyid"":""1yaqkl5trbhvcw"",""name"":""浮动层上"",""style"":{""height"":514,""width"":320},""layers"":[]},{""id"":null,""className"":null,""keyid"":""2xdp378n1aylmo"",""name"":""浮动层下"",""style"":{""height"":514,""width"":320},""layers"":[]}],""popups"":[],""pages"":[{""id"":null,""className"":null,""keyid"":""3211mt6rpwdl34"",""name"":""空白页面"",""desc"":""页面描述"",""style"":{""width"":320,""height"":514,""backgroundColor"":""rgb(255, 255, 255)"",""backgroundRepeat"":""no-repeat"",""backgroundImageCrop"":""{}"",""backgroundGlobal"":false,""backgroundSize"":""100% 100%"",""backgroundImage"":""url(/img/bg.jpg)""},""layers"":[{""id"":null,""className"":null,""animate"":[],""data"":{""data"":""@value5"",""fontFamilySet"":{""name"":""默认字体"",""url"":""""},""style"":{""letterSpacing"":""normal"",""color"":""rgba(0,0,0,1)"",""textDecoration"":""normal"",""textAlign"":""left"",""fontFamily"":""默认字体"",""fontSize"":""18px""},""artword"":{""type"":"""",""name"":"""",""colors"":[]},""animate"":"""",""keyid"":""15g34cdekv4000""},""style"":{""width"":132,""height"":27,""top"":173,""left"":101},""estyle"":{},""events"":[],""name"":""文本"",""pid"":""h5ds_text"",""version"":""1.0.0"",""keyid"":""93rq9u81k4c00"",""set"":{""hide"":false,""lock"":false}},{""id"":null,""className"":null,""animate"":[],""data"":{""data"":""@value4"",""fontFamilySet"":{""name"":""默认字体"",""url"":""""},""style"":{""letterSpacing"":""normal"",""color"":""rgba(0,0,0,1)"",""textDecoration"":""normal"",""textAlign"":""left"",""fontFamily"":""默认字体"",""fontSize"":""18px""},""artword"":{""type"":"""",""name"":"""",""colors"":[]},""animate"":"""",""keyid"":""15g34cdekv4000""},""style"":{""width"":132,""height"":27,""top"":136,""left"":101},""estyle"":{},""events"":[],""name"":""文本"",""pid"":""h5ds_text"",""version"":""1.0.0"",""keyid"":""1n9qzwvfax5s00"",""set"":{""hide"":false,""lock"":false}},{""id"":null,""className"":null,""animate"":[],""data"":{""data"":""@value3"",""fontFamilySet"":{""name"":""默认字体"",""url"":""""},""style"":{""letterSpacing"":""normal"",""color"":""rgba(0,0,0,1)"",""textDecoration"":""normal"",""textAlign"":""left"",""fontFamily"":""默认字体"",""fontSize"":""18px""},""artword"":{""type"":"""",""name"":"""",""colors"":[]},""animate"":"""",""keyid"":""15g34cdekv4000""},""style"":{""width"":132,""height"":27,""top"":97,""left"":101},""estyle"":{},""events"":[],""name"":""文本"",""pid"":""h5ds_text"",""version"":""1.0.0"",""keyid"":""59ptkums9yw000"",""set"":{""hide"":false,""lock"":false}},{""id"":null,""className"":null,""animate"":[],""data"":{""data"":""@value2"",""fontFamilySet"":{""name"":""默认字体"",""url"":""""},""style"":{""letterSpacing"":""normal"",""color"":""rgba(0,0,0,1)"",""textDecoration"":""normal"",""textAlign"":""left"",""fontFamily"":""默认字体"",""fontSize"":""18px""},""artword"":{""type"":"""",""name"":"""",""colors"":[]},""animate"":""""},""style"":{""width"":132,""height"":27,""top"":59,""left"":101},""estyle"":{},""events"":[],""name"":""文本"",""pid"":""h5ds_text"",""version"":""1.0.0"",""keyid"":""1277b6mmlshc00"",""set"":{""hide"":false,""lock"":false}},{""id"":null,""className"":null,""animate"":[],""data"":{""data"":""@value1"",""fontFamilySet"":{""name"":""默认字体"",""url"":""""},""style"":{""letterSpacing"":""normal"",""color"":""rgba(0,0,0,1)"",""textDecoration"":""normal"",""textAlign"":""left"",""fontFamily"":""默认字体"",""fontSize"":""18px""},""artword"":{""type"":"""",""name"":"""",""colors"":[]},""animate"":"""",""keyid"":""15g34cdekv4000""},""style"":{""width"":132,""height"":27,""top"":17,""left"":101},""estyle"":{},""events"":[],""name"":""文本"",""pid"":""h5ds_text"",""version"":""1.0.0"",""keyid"":""i79a57ped0w00"",""set"":{""hide"":false,""lock"":false}}],""slider"":{""animate"":1,""autoplay"":false,""lock"":false,""time"":5}}],""plugins"":[]}";
                if (data.Content.NotNullOrWhiteSpace())
                   await _serverInfoRepository.InsertAsync(new H5ServerInfo { Id = IdGenerator.Id, Code = "defultData", Content = data.Content, CreateTime = DateTime.Now, IsDelete = 0 });
            }
            else
            {
                data.Content = defultData;
            }
        }
        restful.Data = data;
        restful.Code = CodeStatus.OK;
        return restful;
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("H5DS/submit")]
    public async Task<RestfulResult> SubmitAsync([FromBody] H5Template data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        var isExist = await _thisRepository.IsAnyAsync(u => u.Id == data.Id /*&& u.Creator == _currentUser.UserId*/);
        if (!isExist)
        {
            restful.Message = ErrorEnum.D3000.GetDescription(i18n.ResourceManager);
            return restful;
        }
        bool result = await _thisRepository.UpdateAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }
}

